package com.kingstar.ceres.runable;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingstar.ceres.asny.ParseAndSendToSave;
import com.kingstar.ceres.constants.CommonConstants;
import com.kingstar.ceres.constants.DbEntityEnum;
import com.kingstar.ceres.entity.master.*;
import com.kingstar.ceres.mapper.base.BaseDao;
import com.kingstar.ceres.mapper.master.CfetsCmdsStandardRateDao;
import com.kingstar.ceres.memory.MessageMemory;
import com.kingstar.ceres.utils.ApplicationContextUtil;
import com.kingstar.ceres.utils.ParseMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class InitConfig implements ApplicationRunner {

    @Value("${com.kingstar.ceres.pageSize}")
    public Integer pageSize;

    @Value("${com.kingstar.ceres.beginDate}")
    public String beginDate;

    @Value("${com.kingstar.ceres.endDate}")
    public String endDate;

    @Autowired
    CfetsCmdsStandardRateDao cfetsCmdsStandardRateDao;

    @Autowired
    ParseAndSendToSave parseAndSendToSave;

    @Autowired
    ApplicationContextUtil applicationContextUtil;


    @Value("${com.kingstar.ceres.query.memorySize}")
    public Integer memorySize;

    @Value("${com.kingstar.ceres.query.waitTime}")
    public Long waitTime;

    @Value("${com.kingstar.ceres.parse.queueSize}")
    private int queueSize;



    @Override
    public void run(ApplicationArguments args) throws Exception {

        Date beginTime = null;
        Date endTime = null;
        if (StringUtils.isNotBlank(beginDate) || StringUtils.isNotBlank(endDate)){
//            DbEntityEnum.buildWrapper(beginDate,endDate);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            try {
                if (StringUtils.isNotBlank(beginDate)){
                    beginTime = simpleDateFormat.parse(beginDate);
                }
                if (StringUtils.isNotBlank(endDate)){
                    endTime = simpleDateFormat.parse(endDate);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        long begin = System.currentTimeMillis();
        log.info("begin==========="+begin);
//        IPage iPage=null;
        for (DbEntityEnum dbEntityEnum:DbEntityEnum.values()){
            Class mapperClass = dbEntityEnum.getMapperClass();
//            BaseMapper bean =applicationContextUtil.getBean(mapperClass);
//            QueryWrapper wrapper = dbEntityEnum.getWrapper();
//            Integer count = bean.selectCount(wrapper);

            BaseDao baseDao = applicationContextUtil.getBean(mapperClass);
            Integer count = baseDao.count(beginTime, endTime);
            int page=count%pageSize==0?count/pageSize:count/pageSize+1;
            List list=null;
            for (int i = 0; i < page; i++) {
//                iPage=new Page(i+1,pageSize);
//                bean.selectPage(iPage,wrapper);
//                List records = iPage.getRecords();
//                parseList(records);
//                records.clear();

                list = baseDao.pageQuery(i*pageSize, pageSize,beginTime, endTime);
                parseList(list);
                list.clear();
                while (MessageMemory.XSWAP_MARKET_DEAL_QUEUE.size()>memorySize
                        || MessageMemory.XBOND_MARKET_DEAL_QUEUE.size()>memorySize
                        || MessageMemory.MARKET_DATA_SUMMARY_BOND_QUEUE.size()>memorySize
                        || MessageMemory.BOND_MARKET_DEAL_QUEUE.size()>memorySize
                        || MessageMemory.IRS_MARKET_DEAL_QUEUE.size()>memorySize
                        || queueSize<=pageSize+1+CommonConstants.THREAD_POOL_QUEUE.size()
                ){
                    log.info("+++++++++++++++++++{}+++++++++++++++", CommonConstants.THREAD_POOL_QUEUE.size());
                    Thread.sleep(waitTime);
                }
            }
        }

        long end = System.currentTimeMillis();
        log.info("end==========="+end);
        log.info("time consumer============================"+(end-begin));
    }

    public void parseList(List list){
        if (list==null || list.size()==0){
            return;
        }
        for (Object obj : list) {
            if (obj instanceof CfetsCmdsStandardRate){
                CfetsCmdsStandardRate entity=(CfetsCmdsStandardRate)obj;
                String imixtext = entity.getImixtext();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
                continue;
            }
//            if (obj instanceof CfetsRmbCmdsMarketdataSendRecord){
//                CfetsRmbCmdsMarketdataSendRecord entity=(CfetsRmbCmdsMarketdataSendRecord)obj;
//                String imixtext = entity.
//                continue;
//            }
            if (obj instanceof CfetsRmbCmdsMsgDown){
                CfetsRmbCmdsMsgDown entity=(CfetsRmbCmdsMsgDown)obj;
                String imixtext = entity.getImix();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
                continue;
            }
            if (obj instanceof CfetsRmbCmdsMsgDownHistory){
                CfetsRmbCmdsMsgDownHistory entity=(CfetsRmbCmdsMsgDownHistory)obj;
                String imixtext = entity.getImix();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
                continue;
            }
//            if (obj instanceof CfetsRmbCmdsMsgmSendRecord){
//                CfetsRmbCmdsMsgmSendRecord entity=(CfetsRmbCmdsMsgmSendRecord)obj;
//                String imixtext = entity.
//                continue;
//            }
            if (obj instanceof CfetsRmbTradeMsgDown){
                CfetsRmbTradeMsgDown entity=(CfetsRmbTradeMsgDown)obj;
                String imixtext = entity.getImix();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
                continue;
            }
            if (obj instanceof CfetsRmbTradeMsgDownHistory){
                CfetsRmbTradeMsgDownHistory entity=(CfetsRmbTradeMsgDownHistory)obj;
                String imixtext = entity.getImix();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
                continue;
            }
            if (obj instanceof CfetsRmbTradeMsgUp){
                CfetsRmbTradeMsgUp entity=(CfetsRmbTradeMsgUp)obj;
                String imixtext = entity.getImix();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
                continue;
            }
            if (obj instanceof CfetsRmbTradeMsgUpHistory){
                CfetsRmbTradeMsgUpHistory entity=(CfetsRmbTradeMsgUpHistory)obj;
                String imixtext = entity.getImix();
                parseAndSendToSave.parseMessageAndSaveToDB(ParseMsgUtil.parseStrToMessage(imixtext));
            }
        }
    }
}
